package com.oritsh.visitsys.dao;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * WebClient util class
 * 
 * @author ivan
 * 
 */
public class WebClient {
	/**
	 * String url used by connect Set before connect
	 * 
	 * @see connect
	 */
	public String url = "";
	public String postData = "";

	/**
	 * Method used by connection Set before connect
	 * 
	 * @see connect
	 */
	public String method = "POST";

	/**
	 * String posted if method is POST Set before connect
	 * 
	 * @see method
	 * @see connect
	 */
	public String post = "";

	/**
	 * cookie data Get after connect Set before connect
	 * 
	 * @see connect
	 */
	public String cookie = "";

	/**
	 * Connection used Call setRequestProperty after connect
	 * 
	 * @see connect
	 */
	public HttpURLConnection conn;

	public WebClient(String url,String postData)
	{
		this.url = url;
		this.postData = postData;

	}

	public WebClient() {
	}

	/**
	 * Connect to URL specified by url; must set url before calling it
	 * 
	 * @see url
	 */
	public void connect() {
		try {
			URL u = new URL(url);
			conn = (HttpURLConnection) u.openConnection();

			conn.setDoOutput(method.equals("POST"));
			conn.setDoInput(true);

			conn.setRequestMethod(method);
			//conn.setRequestProperty("User-Agent", "Internet Explorer");
			conn.setRequestProperty("Accept", "application/json");
			conn.setRequestProperty("Content-type", "application/json");
			//conn.setRequestProperty("Cache-Control", "no-cache");
			//conn.setRequestProperty("Cookie", cookie);
			
			if (method.equals("POST")) {
				OutputStreamWriter out = new OutputStreamWriter(
						conn.getOutputStream(), "UTF-8");
				//conn.getOutputStream().write(postData);
				//conn.getOutputStream().flush();
				//conn.getOutputStream().close();
				out.append(postData);
	            out.flush();
	            out.close();
			}
			conn.connect();
			cookie = conn.getHeaderField("Set-Cookie");
		} catch (Exception e) {
			e.printStackTrace();
			disconnect();
		}
	}

	/**
	 * Download String using default encoding Call after connect
	 * 
	 * @return downloaded string
	 */
	public String downloadString() {
		return downloadString(null);
	}

	/**
	 * Download String using encoding Call after connect
	 * 
	 * @return downloaded string
	 */
	public String downloadString(String encoding) {
		if (conn == null) {
			return null;
		}
		InputStreamReader isr;
		try {
			if (encoding != null) {
				isr = new InputStreamReader(conn.getInputStream(), encoding);
			} else {
				isr = new InputStreamReader(conn.getInputStream());
			}
			java.io.BufferedReader br = new BufferedReader(isr);
			String line;
			String ret = "";
			while (null != (line = br.readLine())) {
				ret += line + "\n";
			}
			return ret;
		} catch (IOException e) {
			e.printStackTrace();
			disconnect();
			return null;
		}
	}

	/**
	 * Stop the connection and clear resources
	 */
	public void disconnect() {
		if (conn != null) {
			conn.disconnect();
			conn = null;
		}
	}

	public static java.io.InputStream OpenStream(String url)
			throws MalformedURLException, IOException {
		return new URL(url).openStream();
	}
}
